package com.cykj.pos.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.BizPosMessage;
import com.cykj.pos.domain.dto.CashbackRecordDTO;
import com.cykj.pos.domain.dto.MerchantCloseDTO;
import com.cykj.pos.domain.vo.BizMerchParentRelated;
import com.cykj.pos.domain.vo.BizeServerCharge;
import com.cykj.pos.mapper.BizMerchTransactionsMapper;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.pos.mapper.BizTransactionAmountMapper;
import com.cykj.pos.mapper.UnFailureActiveTemplateMapper;
import com.cykj.pos.service.*;
import com.cykj.pos.util.DESHelperUtil;
import com.cykj.pos.util.UUIDUtil;
import com.cykj.system.service.ISysUserService;
import io.netty.resolver.InetNameResolver;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BizTransactionAmountServiceImpl implements IBizTransactionAmount {

    @Resource
    private IBizFeesChargedService bizFeesChargedService;
    @Resource
    private BizTransactionAmountMapper bizTransactionAmountMapper;

    @Resource
    private IBizPosMachineService bizPosMachineService;

    @Resource
    private IBizMerchantService bizMerchantService;

    @Resource
    private IBizTerminalRateListService bizTerminalRateListService;

    @Resource
    private IBizWalletService walletService;

    @Resource
    private IBizMessageRecordsService bizMessageRecordsService;

    @Resource
    private IBizMerchBillService bizMerchBillService;

    @Resource
    private BizMerchTransactionsMapper merchTransactionsMapper;

    @Resource
    private IBizTransactionAmountThisMonthService iBizTransactionAmountThisMonthService;

    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private IBizMerchantInfoService iBizMerchantInfoService;

    @Resource
    private BizPosMachineMapper bizPosMachineMapper;

    @Resource
    private UnFailureActiveTemplateMapper unFailureActiveTemplateMapper;

    @Resource
    private IBizPosMachineStatusRecordsService iBizPosMachineStatusRecordsService;

    @Resource
    private IAizStatisticalMerchantService iAizStatisticalMerchantService;

    @Resource
    private IAizDeductionRecordsService iAizDeductionRecordsService;
//
    @Resource
    private IAizUnActiveRecordsService iAizUnActiveRecordsService;


    @Override
    public String insertBizTransactionAmount(SynchronizationDataDTO synchronizationDataDTO)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            synchronizationDataDTO.setHostDate(simpleDateFormat.parse(synchronizationDataDTO.getHost_date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //sn查询 商户merchId
        BizPosMachine posMachineBySnCode = bizPosMachineService.getPosMachineBySnCode(synchronizationDataDTO.getPos_sn());
        //查询直属上级id
        String uuid = IdWorker.getIdStr();
        if (posMachineBySnCode == null) {
            BizPosMachine posMachine = new BizPosMachine();
            posMachine.setMerchId(0L);
            synchronizationDataDTO.setUuid(uuid);
            synchronizationDataDTO.setMerchId(posMachine.getMerchId());
            synchronizationDataDTO.setParentId(0L);
        } else {
            Long parentId = bizPosMachineService.selectParentId(posMachineBySnCode.getMerchId());
            synchronizationDataDTO.setUuid(uuid);
            synchronizationDataDTO.setMerchId(posMachineBySnCode.getMerchId());
            synchronizationDataDTO.setParentId(parentId);
        }
        bizTransactionAmountMapper.insertBizTransactionAmount(synchronizationDataDTO);//存今日
        bizTransactionAmountMapper.insertBizTransactionAmountHistory(synchronizationDataDTO);//存历史
        return uuid;
    }

    /**
     * 分账方法
     *
     * @param sy
     * @return
     */
    @Override
    @Transactional
    public String castAccount(SynchronizationDataDTO sy) {
        if("1".equals(sy.getSilenceType())){
            // 算账之前 先存一下交易表 确保之前数据正确
            sy.setIsSecondExecute("0");
            Boolean b = shareTheMoney(sy);
            sy.setAddi_amount(sy.getActualAddiAmount());
            sy.setFee_name(sy.getActualFeeName());
            sy.setIsSecondExecute("1");
            shareTheMoney(sy);
            if (b) {
                return "ok";
            } else {
                return "off";
            }
        }else{
            sy.setIsSecondExecute("1");
            Boolean b = shareTheMoney(sy);
            if (b) {
                return "ok";
            } else {
                return "off";
            }
        }
    }

    @Override
    public String insertBizTransactions(SynchronizationDataDTO synchronizationDataDTO) {
        String uuid = IdWorker.getIdStr();
        synchronizationDataDTO.setUuid(uuid);
        synchronizationDataDTO.setWhetherSteal(1);
        bizTransactionAmountMapper.insertBizTransactions(synchronizationDataDTO);
        return uuid;

    }

    @Override
    public int updateBizTransactionAmount(BizMerchTransactions bizMerchTransactions) {
        return bizTransactionAmountMapper.updateBizTransactionAmount(bizMerchTransactions);
    }

    @Override
    public int updateBizTransactionAmountSuffix(BizMerchTransactions bizMerchTransactions) {
        return bizTransactionAmountMapper.updateBizTransactionAmountSuffix(bizMerchTransactions);
    }

    @Override
    public int updateBizMerchTransactions(BizMerchTransactions bizMerchTransactions) {
        return bizTransactionAmountMapper.updateBizMerchTransactions(bizMerchTransactions);
    }

    @Override
    public BizPosMessage selectPos(BizMerchTransactions bizMerchTransactions) {
        return bizTransactionAmountMapper.selectPos(bizMerchTransactions);
    }

    @Override
    public List<BizPosMessage> selectPosSy() {
        /*返回的列表  指未分账的*/
        List<BizPosMessage> bizPosMessagesReturn=new ArrayList<BizPosMessage>();
        /*查询所有一段时间之后的数据*/
        List<BizPosMessage> bizPosMessage= bizTransactionAmountMapper.selectPosSy();
        for (BizPosMessage bizPosMessage1: bizPosMessage){
            if (bizPosMessage1.getTransType().equals("291")){
                int bizProfitDeatailsCounts=merchTransactionsMapper.selectBizProfitDeatailCount291(bizPosMessage1.getUuid());
                if (bizProfitDeatailsCounts==0){
                    bizPosMessagesReturn.add(bizPosMessage1);
                }
            }else {
                int bizProfitDeatailsCounts=merchTransactionsMapper.selectBizProfitDeatailCount(bizPosMessage1.getUuid());
                if (bizProfitDeatailsCounts==0){
                    bizPosMessagesReturn.add(bizPosMessage1);
                }
            }
        }
        return bizPosMessagesReturn;
    }

    @Override
    public int updateBizTransactionAmountAddiAmount(BizPosMessage syDTO) {
        return bizTransactionAmountMapper.updateBizTransactionAmountAddiAmount(syDTO);
    }

    @Override
    public int insertBizRateMoney(BizPosMessage syDTO) {


        return bizTransactionAmountMapper.insertBizRateMoney(syDTO);
    }

    @Override
    public List<String> selectAllPos() {
        return bizTransactionAmountMapper.selectAllPos();
    }

    @Override
    public BizMerchParentRelated selectParenId(String posCode) {
        return bizTransactionAmountMapper.selectParenId(posCode);
    }

    @Override
    public void updateBizTransactionAmountMerchIdAndParentId(BizMerchParentRelated bizMerchParentRelated) {
        bizTransactionAmountMapper.updateBizTransactionAmountMerchIdAndParentId(bizMerchParentRelated);
    }

    @Override
    public String cashMoney(SynchronizationDataDTO sy) {
        BizPosMachine bizPosMachine = bizPosMachineService.selectPosBySnCode(sy.getPos_sn());
        Long merchId = bizPosMachine.getMerchId();
        String amount = String.valueOf(sy.getAmnount()).replace(".0", "").replace(".00", "");
        String childMoney = "0";
        List<CashbackRecordDTO> merchs = new ArrayList<CashbackRecordDTO>();
        for (int i = 1; i > 0; i++) {
            //查询商户开关开没开
            BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(merchId);
            //查询给多少钱
           /* String syAmount = sy.getAmnount();
            int i1 = syAmount.indexOf(".");*/
            String money = String.valueOf(sy.getAmnount()).replace(".0", "").replace(".00", "");
            String serverMoney = "0";
            BizMerchantReturnMoney bizMerchantReturnMoney = bizMerchantService.selectBizServiceCharge(bizMerchant.getUserId());
            CashbackRecordDTO cashbackRecordDTO = new CashbackRecordDTO();
            if (bizMerchantReturnMoney.getServerSwitch().equals("1") && bizMerchantReturnMoney != null) {
                if(money.equals("24") ||money.equals("36") || money.equals("48")  || money.equals("49") || money.equals("60") || money.equals("99") || money.equals("199") || money.equals("299")|| money.equals("399")) { //有匹配的费率才进来  没有直接找下一个
                    if (money.equals("24") && !bizMerchantReturnMoney.getServerThirtySix().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerTwoFour()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerTwoFour();
                    }
                    if (money.equals("36") && !bizMerchantReturnMoney.getServerThirtySix().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerThirtySix()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerThirtySix();
                    }
                    if (money.equals("48") && !bizMerchantReturnMoney.getServerFortyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerFortyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerFortyNine();
                    }
                    if (money.equals("49") && !bizMerchantReturnMoney.getServerFortyEight().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerFortyEight()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerFortyEight();

                    }
                    if (money.equals("60") && !bizMerchantReturnMoney.getServerSixty().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerSixty()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerSixty();
                    }
                    if (money.equals("99") && !bizMerchantReturnMoney.getServerNinetyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerNinetyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerNinetyNine();
                    }
                    if (money.equals("199") && !bizMerchantReturnMoney.getServerOneNinetyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerOneNinetyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerOneNinetyNine();
                    }
                    if (money.equals("299") && !bizMerchantReturnMoney.getServerTwoNinetyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerTwoNinetyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerTwoNinetyNine();
                    }
                    if (money.equals("399") && !bizMerchantReturnMoney.getServerThreeNinetyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getServerThreeNinetyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getServerThreeNinetyNine();
                    }
                    cashbackRecordDTO.setMerchId(bizMerchant.getMerchId());
                    cashbackRecordDTO.setUserId(bizMerchant.getUserId());
                    cashbackRecordDTO.setAmount(String.valueOf(sy.getAmnount()));
                    cashbackRecordDTO.setPosCode(sy.getPos_sn());
                    //交易进来的钱需要和应该给的钱需要比较 谁小给谁
                    if (new BigDecimal(amount).compareTo(new BigDecimal(serverMoney)) == 1) {
                        cashbackRecordDTO.setReturnMoney(serverMoney);//应该返现的钱
                    } else {
                        cashbackRecordDTO.setReturnMoney(amount);//应该返现的钱
                    }
                    cashbackRecordDTO.setUuid(sy.getUuid());
                    cashbackRecordDTO.setMerchName(bizMerchant.getMerchName());
                    cashbackRecordDTO.setHostDate(String.valueOf(sy.getHost_date()));
                    merchs.add(cashbackRecordDTO);
                }
            }
            merchId = bizMerchant.getParentId();
            BigDecimal zero = new BigDecimal("0.00");
            if (bizMerchantReturnMoney.getServerSwitch().equals("1")) {
                BigDecimal subtract = new BigDecimal(amount).subtract(new BigDecimal(serverMoney));
                amount = subtract.toString();
            }
            if (new BigDecimal(amount).compareTo(zero) < 1) {
                break;
            }
            if (bizMerchant.getMerchId() == 2) {// 服务中心 服务中心的钱也算
                break;
            }

        }
        returnMoney(merchs, String.valueOf(sy.getHost_date()));
        return "ok";
    }

    @Override
    public SynchronizationDataDTO selectOne(String uuid) {
        return bizTransactionAmountMapper.selectOne(uuid);
    }

    @Override
    public String castAccountTrack(SynchronizationDataDTO sy) {
        // 算账之前 先存一下交易表 确保之前数据正确
        Boolean b = shareTheMoneyTrack(sy);
        if (b) {
            return "ok";
        } else {
            return "off";
        }
    }

    @Override
    public void updateBizTransactionAmountPos(SynchronizationDataDTO synchronizationDataDTO) {
        bizTransactionAmountMapper.updateBizTransactionAmountPos(synchronizationDataDTO);
    }
    @Override
    public String selectSuffix(BizMerchTransactions bizMerchTransactions) {
        return bizTransactionAmountMapper.selectSuffix(bizMerchTransactions);
    }

    @Override
    public String  returnSimAmt(SynchronizationDataDTO sy,boolean kg) {
        Long deptId=bizTransactionAmountMapper.selectDeptIdByPosCode(sy.getPos_sn());
        //秒付通60流量费不进行实际返现
        if(kg&&(deptId==118||deptId==119)&&sy.getSim_amt()==60){
            return "";
        }
        if(kg&&deptId==117){
            String registerTime=bizTransactionAmountMapper.getregisterTime(sy.getMerchant_no());
            if(StringUtils.isNotBlank(registerTime)){
                Long time=10*30*24*60*60*1000l;
                Date now=new Date();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                try {
                    now=sdf.parse(sdf.format(now));
                    Date register=sdf.parse(registerTime);
                    if(now.getTime()-register.getTime()>time){
                        bizTransactionAmountMapper.insertCZRZ(sy.getPos_sn(),sy.getMerchant_no(),sy.getUuid(),1);
                        return "ok";
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            int iss=bizFeesChargedService.getFeeChargeServiceByPosCode(sy.getPos_sn(),sy.getMerchant_no());
            if(iss>1){
                bizTransactionAmountMapper.insertCZRZ(sy.getPos_sn(),sy.getMerchant_no(),sy.getUuid(),2);
                return "ok";
            }
        }
        if((!kg)&&(deptId==118||deptId==119||deptId==117)){
            int isBack=bizTransactionAmountMapper.selectBackBySn(sy.getPos_sn());
            if(isBack>0){
                bizTransactionAmountMapper.insertCZRZ(sy.getPos_sn(),sy.getMerchant_no(),sy.getUuid(),3);
                return "ok";
            }
        }
        BizPosMachine bizPosMachine = bizPosMachineService.selectPosBySnCode(sy.getPos_sn());
        Long merchId = bizPosMachine.getMerchId();
        String amount = String.valueOf(sy.getSim_amt()); //流量费
        String childMoney = "0";
        List<CashbackRecordDTO> merchs = new ArrayList<CashbackRecordDTO>();
        boolean b=false;
        for (int i = 1; i > 0; i++) {
            //查询商户开关开没开
            BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(merchId);
            if(bizMerchant==null){
                b=true;
                break;
            }
            //查询给多少钱
           /* String syAmount = sy.getAmnount();
            int i1 = syAmount.indexOf(".");*/
            String money = String.valueOf(sy.getSim_amt()).replace(".0", "").replace(".00", "");
            String serverMoney = "";
            BizMerchantReturnMoney bizMerchantReturnMoney = bizMerchantService.selectBizServiceCharge(bizMerchant.getUserId());
            CashbackRecordDTO cashbackRecordDTO = new CashbackRecordDTO();
            if (bizMerchantReturnMoney.getServerSwitch().equals("1") && bizMerchantReturnMoney != null) {
                if(money.equals("24") || money.equals("36") || money.equals("48") || money.equals("60") || money.equals("99")|| money.equals("68")|| money.equals("77")) { //有匹配的费率才进来  没有直接找下一个
                    if (money.equals("24") && !bizMerchantReturnMoney.getFlowTwoFour().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowTwoFour()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowTwoFour();
                    }
                    if (money.equals("36") && !bizMerchantReturnMoney.getFlowThirtySix().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowThirtySix()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowThirtySix();

                    }
                    if (money.equals("48") && !bizMerchantReturnMoney.getFlowFortyEight().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowFortyEight()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowFortyEight();
                    }
                    if (money.equals("60") && !bizMerchantReturnMoney.getFlowSixty().equals("")) {
                        if("9".equals(bizPosMachine.getChannel())){
                            serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowSixtyHyb()).subtract(new BigDecimal(childMoney)).toString();
                            childMoney = bizMerchantReturnMoney.getFlowSixtyHyb();
                        }else if ("10".equals(bizPosMachine.getChannel())){
                            serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowSixtyLkl()).subtract(new BigDecimal(childMoney)).toString();
                            childMoney = bizMerchantReturnMoney.getFlowSixtyLkl();
                        }else{
                            serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowSixty()).subtract(new BigDecimal(childMoney)).toString();
                            childMoney = bizMerchantReturnMoney.getFlowSixty();
                        }
                    }
                    if (money.equals("99") && !bizMerchantReturnMoney.getFlowNinetyNine().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowNinetyNine()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowNinetyNine();
                    }
                    if (money.equals("68") && !bizMerchantReturnMoney.getFlowSixtyEight().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowSixtyEight()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowSixtyEight();
                    }
                    if (money.equals("77") && !bizMerchantReturnMoney.getFlowSeventySeven().equals("")) {
                        serverMoney = new BigDecimal(bizMerchantReturnMoney.getFlowSeventySeven()).subtract(new BigDecimal(childMoney)).toString();
                        childMoney = bizMerchantReturnMoney.getFlowSeventySeven();
                    }
                    cashbackRecordDTO.setMerchId(bizMerchant.getMerchId());
                    cashbackRecordDTO.setUserId(bizMerchant.getUserId());
                    cashbackRecordDTO.setAmount(money);
                    cashbackRecordDTO.setPosCode(sy.getPos_sn());
                    //交易进来的钱需要和应该给的钱需要比较 谁小给谁
                    if (new BigDecimal(amount).compareTo(new BigDecimal(serverMoney)) == 1) {
                        cashbackRecordDTO.setReturnMoney(serverMoney);//应该返现的钱
                    } else {
                        cashbackRecordDTO.setReturnMoney(amount);//应该返现的钱
                    }
                    cashbackRecordDTO.setUuid(sy.getUuid());
                    cashbackRecordDTO.setMerchName(bizMerchant.getMerchName());
                    cashbackRecordDTO.setHostDate(String.valueOf(sy.getHost_date()));
                    merchs.add(cashbackRecordDTO);
                }
            }
            merchId = bizMerchant.getParentId();
            BigDecimal zero = new BigDecimal("0.00");
            if (bizMerchantReturnMoney.getServerSwitch().equals("1")) {
                BigDecimal subtract = new BigDecimal(amount).subtract(new BigDecimal(serverMoney));
                amount = subtract.toString();
            }
            if (new BigDecimal(amount).compareTo(zero) < 1) {
                break;
            }
            if (bizMerchant.getMerchId() == 2) {// 服务中心 服务中心的钱也算
                break;
            }

        }
        if(b){
            return "ok";
        }
        backSimAmt(merchs, String.valueOf(sy.getHost_date()));
        return "ok";

    }


    private void backSimAmt(List<CashbackRecordDTO> merchs, String host_date) {

        //向钱包中打钱并且生成账单
        if (merchs.size()>0){
            for (CashbackRecordDTO list : merchs) {
                if(!"0".equals(list.getReturnMoney()) || new BigDecimal(list.getReturnMoney()).compareTo(new BigDecimal(0)) == -1){  //返现的钱不能位0  是0不给钱不生成账单
                    BizWallet bizWallet1 = new BizWallet();
                    BizWallet bizWallet = walletService.getMyWalletByUserId(list.getUserId());
                    String rewardAmount = bizWallet.getRewardAmount(); //奖励余额
                    String walletAmount = bizWallet.getWalletAmount();  // 总余额
                    //解密
                    String secretKey = bizWallet.getSecretKey();
                    String decrypt = DESHelperUtil.decrypt(secretKey, rewardAmount);
                    String decrypt1 = DESHelperUtil.decrypt(secretKey, walletAmount);
                    BigDecimal add = new BigDecimal(decrypt).add(new BigDecimal(list.getReturnMoney()));
                    BigDecimal add1 = new BigDecimal(decrypt1).add(new BigDecimal(list.getReturnMoney()));
                    //加密
                    String encrypt = DESHelperUtil.encrypt(secretKey, add.toString());
                    String encrypt1 = DESHelperUtil.encrypt(secretKey, add1.toString());
                    bizWallet1.setRewardAmount(encrypt);
                    bizWallet1.setWalletAmount(encrypt1);
                    bizWallet1.setUserId(list.getUserId());
                    // 推送消息
                    BizMessageRecords bizMessageRecords = new BizMessageRecords();
                    bizMessageRecords.setMsgUserId(list.getUserId());
                    bizMessageRecords.setMsgContent("SN:"+list.getPosCode()+",流量费返现金额:"+list.getReturnMoney()+"元");
                    bizMessageRecords.setMsgStatus(0);
                    bizMessageRecords.setMsgType(2);
                    bizMessageRecords.setReadStatus(0);
                    bizMessageRecords.setAdviceType(1);
                    bizMessageRecordsService.save(bizMessageRecords);
                    //更新钱包
                    int row = walletService.updateWalletByUserId(bizWallet1);
                    list.setStatu("2");
                    //保存服务费账单
                    int row1 = walletService.insertBizServiceBills(list);
                    bizFeesChargedService.updateStau(list.getUuid());
                    //保存账单
                    BizMerchBill bizMerchBill = new BizMerchBill();
                    bizMerchBill.setMerchId(list.getMerchId());
                    bizMerchBill.setMerchName(list.getMerchName());
                    bizMerchBill.setBillType("6");
                    bizMerchBill.setAmount(new BigDecimal(list.getReturnMoney()));
                    bizMerchBill.setBillDate(list.getHostDate());
                    bizMerchBillService.save(bizMerchBill);
                }

            }
        }


    }

    private Boolean shareTheMoneyTrack(SynchronizationDataDTO sy) {
        String snCode = sy.getPos_sn();// 设备号
        // String cardType = sy.getCard_type();// 卡类型
        BizPosMachine bizPosMachine = bizPosMachineService.selectPosBySnCode(snCode);
        List<BizPosErminalRate> erminalRates = bizPosMachineService.selectPosErminalRate();
        String credit = "";
       /* for (BizPosErminalRate rate : erminalRates) {
            if (rate.getFeeChlName().equals(sy.getFee_name())) {
                credit = rate.getFeeChlValue();// 拿出费率
            }
        }*/
        Long merchId = bizPosMachine.getMerchId();
        List<MerchantCloseDTO> merchs = new ArrayList<MerchantCloseDTO>();
        for (int i = 1; i > 0; i++) {
            BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(merchId);
            MerchantCloseDTO merchantCloseDTO = new MerchantCloseDTO();
            merchantCloseDTO.setMerchId(bizMerchant.getMerchId());
            merchantCloseDTO.setSnCode(snCode);
            merchantCloseDTO.setName(bizMerchant.getMerchName());
            merchantCloseDTO.setUserId(bizMerchant.getUserId());
            // 查询他的费率
            BizTerminalRateList bizTerminalRateList = bizTerminalRateListService.getBizTerminalRateListServiceByUserId(bizMerchant.getUserId());
            // 判读他的交易类型 存费率
            credit = new BigDecimal(sy.getAddi_amount()).divide(new BigDecimal(sy.getAmnount()),10, RoundingMode.DOWN).toString();
            if (new BigDecimal(sy.getAddi_amount()).divide(new BigDecimal(sy.getAmnount()),10, RoundingMode.DOWN).compareTo(new BigDecimal(0.0041)) == 1) {
                String rateT0 = bizTerminalRateListService.getBizRateDataByIdOther(bizTerminalRateList.getRateT0());
                // 信用卡费率
                merchantCloseDTO.setCloseType("001");
                merchantCloseDTO.setAmount(String.valueOf(sy.getAmnount()));
                merchantCloseDTO.setCloseRate(rateT0);// 信用卡费率
                merchantCloseDTO.setTransactionId(bizTerminalRateList.getAccountId());
                merchantCloseDTO.setFee_name(sy.getFee_name());
                merchantCloseDTO.setUuid(sy.getUuid());
            } else {
                String qrsettleRate = bizTerminalRateListService.getBizRateDataByIdOther(bizTerminalRateList.getQrsettleRate());
                // 扫码费率
                merchantCloseDTO.setCloseType("002");
                merchantCloseDTO.setAmount(String.valueOf(sy.getAmnount()));
                merchantCloseDTO.setCloseRate(qrsettleRate);// 扫码费率
                merchantCloseDTO.setTransactionId(bizTerminalRateList.getAccountId());
                merchantCloseDTO.setFee_name(sy.getFee_name());
                merchantCloseDTO.setUuid(sy.getUuid());
            }
            merchs.add(merchantCloseDTO);
            merchId = bizMerchant.getParentId();
            if (bizMerchant.getMerchId() == 2) {// 服务中心 服务中心的钱也算
                break;
            }
        }
        String serviceCharge = String.valueOf(sy.getAddi_amount());// 手续费
        // 算钱
        Boolean b = false;
        // if (merchs.get(0).getCloseType().equals("002")) {
        // 扫码
        //      credit = "0.0038";
        b = calculate2(credit, merchs, serviceCharge, String.valueOf(sy.getHost_date()), sy);
        //  } else {
        // 刷卡
        //       b = calculate(credit, merchs, serviceCharge, sy.getHost_date());
        //    }

        return b;
    }

    private Boolean calculate2(String over, List<MerchantCloseDTO> merchs, String serviceCharge, String hostDate, SynchronizationDataDTO sy) {
        BigDecimal posCredit = new BigDecimal(over);// POS机终端费率
        String amount = merchs.get(0).getAmount();// 总交易金额
        BigDecimal allAmount = new BigDecimal(amount);
        BigDecimal service = new BigDecimal(serviceCharge);// 手续费
        for (int i = 0; i < merchs.size(); i++) {
            String merchCredit = merchs.get(i).getCloseRate();// 结算费率

            if (i == 0) {
                if (posCredit.compareTo(new BigDecimal(merchCredit)) == 0) {
                    merchs.get(i).setCloseMoney(new BigDecimal(0.00));
                    merchs.get(i).setOverMoney(new BigDecimal(0.00));
                } else {
                    // 最下级
                    BigDecimal bigMerch = new BigDecimal(merchCredit);
                    BigDecimal first = allAmount.multiply(bigMerch);// 总交易量x个人费率
                    merchs.get(i).setCloseMoney(first);// 存起上级去算钱
                    BigDecimal end = service.subtract(first);// 手续费-个人比例算的钱
//                    BigDecimal end = new BigDecimal(merchs.get(i).getCloseRate()).multiply(allAmount).subtract(first);
                    merchs.get(i).setOverMoney(end);// 最终应该赚到的钱
                }
            } else {
                // 除了最下级以外的上级
                // String merchCredit = merchs.get(i).getCloseRate();// 结算费率
                BigDecimal bigMerch = new BigDecimal(merchCredit);
                BigDecimal first = allAmount.multiply(bigMerch);// 总交易量x个人费率
                merchs.get(i).setCloseMoney(first);// 存起上级去算钱
//                BigDecimal end = merchs.get(i - 1).getCloseMoney().subtract(first);//下级结算的钱-个人比例算的钱
                BigDecimal up = new BigDecimal(merchs.get(i).getCloseRate()).multiply(allAmount);
                BigDecimal down = new BigDecimal(merchs.get(i - 1).getCloseRate()).multiply(allAmount);
                BigDecimal end = down.subtract(up);
                merchs.get(i).setOverMoney(end);// 最终应该赚到的钱
            }
        }
        // 结算完成 向钱包中打钱
        int row = walletService.savePosOneCloseMoney2(merchs, hostDate,sy);// 存pso1.0结算的钱
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }


    @Transactional
    public void returnMoney(List<CashbackRecordDTO> merchs, String host_date) {
        //向钱包中打钱并且生成账单
        if (merchs.size()>0){
            for (CashbackRecordDTO list : merchs) {
                if(!"0".equals(list.getReturnMoney()) || new BigDecimal(list.getReturnMoney()).compareTo(new BigDecimal(0)) == -1){  //返现的钱不能位0  是0不给钱不生成账单
                    BizWallet bizWallet1 = new BizWallet();
                    BizWallet bizWallet = walletService.getMyWalletByUserId(list.getUserId());
                    String rewardAmount = bizWallet.getRewardAmount(); //奖励余额
                    String walletAmount = bizWallet.getWalletAmount();  // 总余额
                    //解密
                    String secretKey = bizWallet.getSecretKey();
                    String decrypt = DESHelperUtil.decrypt(secretKey, rewardAmount);
                    String decrypt1 = DESHelperUtil.decrypt(secretKey, walletAmount);
                    BigDecimal add = new BigDecimal(decrypt).add(new BigDecimal(list.getReturnMoney()));
                    BigDecimal add1 = new BigDecimal(decrypt1).add(new BigDecimal(list.getReturnMoney()));
                    //加密
                    String encrypt = DESHelperUtil.encrypt(secretKey, add.toString());
                    String encrypt1 = DESHelperUtil.encrypt(secretKey, add1.toString());
                    bizWallet1.setRewardAmount(encrypt);
                    bizWallet1.setWalletAmount(encrypt1);
                    bizWallet1.setUserId(list.getUserId());
                    // 推送消息
                    BizMessageRecords bizMessageRecords = new BizMessageRecords();
                    bizMessageRecords.setMsgUserId(list.getUserId());
                    bizMessageRecords.setMsgContent("SN:"+list.getPosCode()+",返现金额:"+list.getReturnMoney()+"元");
                    bizMessageRecords.setMsgStatus(0);
                    bizMessageRecords.setMsgType(2);
                    bizMessageRecords.setReadStatus(0);
                    bizMessageRecords.setAdviceType(1);
                    bizMessageRecordsService.save(bizMessageRecords);
                    //更新钱包
                    int row = walletService.updateWalletByUserId(bizWallet1);
                    list.setStatu("1");
                    //保存服务费账单
                    int row1 = walletService.insertBizServiceBills(list);
                    //保存账单
                    BizMerchBill bizMerchBill = new BizMerchBill();
                    bizMerchBill.setMerchId(list.getMerchId());
                    bizMerchBill.setMerchName(list.getMerchName());
                    bizMerchBill.setBillType("1");
                    bizMerchBill.setAmount(new BigDecimal(list.getReturnMoney()));
                    bizMerchBill.setBillDate(list.getHostDate());
                    bizMerchBill.setVar5(list.getUuid());
                    bizMerchBillService.save(bizMerchBill);
                }

            }
        }

    }
    public Boolean shareTheMoney(SynchronizationDataDTO sy) {
        String snCode = sy.getPos_sn();// 设备号
        // String cardType = sy.getCard_type();// 卡类型
        BizPosMachine bizPosMachine = bizPosMachineService.selectPosBySnCode(snCode);
        List<BizPosErminalRate> erminalRates = bizPosMachineService.selectPosErminalRate();
        String credit = "";
       /* for (BizPosErminalRate rate : erminalRates) {
            if (rate.getFeeChlName().equals(sy.getFee_name())) {
                credit = rate.getFeeChlValue();// 拿出费率
            }
        }*/
        if(bizPosMachine==null){
            return false;
        }
        Long merchId = bizPosMachine.getMerchId();
        List<MerchantCloseDTO> merchs = new ArrayList<MerchantCloseDTO>();
        for (int i = 1; i > 0; i++) {
            BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(merchId);
            MerchantCloseDTO merchantCloseDTO = new MerchantCloseDTO();
            merchantCloseDTO.setMerchId(bizMerchant.getMerchId());
            merchantCloseDTO.setSnCode(snCode);
            merchantCloseDTO.setName(bizMerchant.getMerchName());
            merchantCloseDTO.setUserId(bizMerchant.getUserId());
            // 查询他的费率
            BizTerminalRateList bizTerminalRateList = bizTerminalRateListService.getBizTerminalRateListServiceByUserId(bizMerchant.getUserId());
            // 判读他的交易类型 存费率
           credit = new BigDecimal(sy.getAddi_amount()).divide(new BigDecimal(sy.getAmnount()),10, RoundingMode.DOWN).toString();
            if (new BigDecimal(sy.getAddi_amount()).divide(new BigDecimal(sy.getAmnount()),10, RoundingMode.DOWN).compareTo(new BigDecimal(0.0041)) == 1) {
                String rateT0 = bizTerminalRateListService.getBizRateDataByIdOther(bizTerminalRateList.getRateT0());
                if(new BigDecimal(rateT0).longValue()>new BigDecimal(credit).longValue()){
                    rateT0=credit;
                }
                // 信用卡费率
                merchantCloseDTO.setCloseType("001");
                merchantCloseDTO.setAmount(String.valueOf(sy.getAmnount()));
                merchantCloseDTO.setCloseRate(rateT0);// 信用卡费率
                merchantCloseDTO.setTransactionId(bizTerminalRateList.getAccountId());
                merchantCloseDTO.setFee_name(sy.getFee_name());
                merchantCloseDTO.setUuid(sy.getUuid());
            } else {
                String qrsettleRate = bizTerminalRateListService.getBizRateDataByIdOther(bizTerminalRateList.getQrsettleRate());
                // 扫码费率
                merchantCloseDTO.setCloseType("002");
                merchantCloseDTO.setAmount(String.valueOf(sy.getAmnount()));
                merchantCloseDTO.setCloseRate(qrsettleRate);// 扫码费率
                merchantCloseDTO.setTransactionId(bizTerminalRateList.getAccountId());
                merchantCloseDTO.setFee_name(sy.getFee_name());
                merchantCloseDTO.setUuid(sy.getUuid());
            }
            merchs.add(merchantCloseDTO);
            merchId = bizMerchant.getParentId();
            if (bizMerchant.getMerchId() == 2) {// 服务中心 服务中心的钱也算
                break;
            }
        }
        String serviceCharge = String.valueOf(sy.getAddi_amount());// 手续费
        // 算钱
        Boolean b = false;
       // if (merchs.get(0).getCloseType().equals("002")) {
            // 扫码
      //      credit = "0.0038";
            b = calculate(credit, merchs, serviceCharge, String.valueOf(sy.getHost_date()), sy);
      //  } else {
            // 刷卡
     //       b = calculate(credit, merchs, serviceCharge, sy.getHost_date());
    //    }

        return b;
    }

    public Boolean calculate(String over, List<MerchantCloseDTO> merchs, String serviceCharge, String hostDate, SynchronizationDataDTO sy) {
        BigDecimal posCredit = new BigDecimal(over);// POS机终端费率
        String amount = merchs.get(0).getAmount();// 总交易金额
        BigDecimal allAmount = new BigDecimal(amount);
        BigDecimal service = new BigDecimal(serviceCharge);// 手续费
        for (int i = 0; i < merchs.size(); i++) {
            String merchCredit = merchs.get(i).getCloseRate();// 结算费率

            if (i == 0) {
                if (posCredit.compareTo(new BigDecimal(merchCredit)) == 0) {
                    merchs.get(i).setCloseMoney(new BigDecimal(0.00));
                    merchs.get(i).setOverMoney(new BigDecimal(0.00));
                } else {
                    // 最下级
                    BigDecimal bigMerch = new BigDecimal(merchCredit);
                    BigDecimal first = allAmount.multiply(bigMerch);// 总交易量x个人费率
                    BigDecimal end = BigDecimal.ZERO;
                    merchs.get(i).setCloseMoney(first);// 存起上级去算钱
                    if(sy.getFee_name().contains("\\+")){
                        String[] split = sy.getFee_name().split("\\+");
                        String plusThree = split[1];
                        if("3".equals(plusThree)){//+3de  先扣除+3  再减
                            end = service.subtract(new BigDecimal(3)).subtract(first);
                        }else{
                            end = service.subtract(first);
                        }
                    }else{
                        end = service.subtract(first);
                    }

                    // 手续费-个人比例算的钱
//                    BigDecimal end = new BigDecimal(merchs.get(i).getCloseRate()).multiply(allAmount).subtract(first);
                    merchs.get(i).setOverMoney(end);// 最终应该赚到的钱
                }
            } else {
                // 除了最下级以外的上级
                // String merchCredit = merchs.get(i).getCloseRate();// 结算费率
                BigDecimal bigMerch = new BigDecimal(merchCredit);
                BigDecimal first = allAmount.multiply(bigMerch);// 总交易量x个人费率
                merchs.get(i).setCloseMoney(first);// 存起上级去算钱
//                BigDecimal end = merchs.get(i - 1).getCloseMoney().subtract(first);//下级结算的钱-个人比例算的钱
                BigDecimal up = new BigDecimal(merchs.get(i).getCloseRate()).multiply(allAmount);
                BigDecimal down = new BigDecimal(merchs.get(i - 1).getCloseRate()).multiply(allAmount);
                BigDecimal end = down.subtract(up);
                merchs.get(i).setOverMoney(end);// 最终应该赚到的钱
            }
        }
        // 结算完成 向钱包中打钱
        int row = walletService.savePosOneCloseMoney(merchs, hostDate,sy);// 存pso1.0结算的钱
        if (row > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public BizFeesCharged createSimAmt(SynchronizationDataDTO sy) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        //创建流量费费用收据列表
        BizFeesCharged bizFeesCharged = new BizFeesCharged();
        bizFeesCharged.setMamountId(IdWorker.getIdStr());
        bizFeesCharged.setOrgnname(sy.getOrgnname());
        bizFeesCharged.setMerchantNo(sy.getMerchant_no());
        bizFeesCharged.setSn(sy.getPos_sn());
        bizFeesCharged.setSimcardfee(sy.getAmnount().toString());
        bizFeesCharged.setMamountStarts("1");
        bizFeesCharged.setAmtType("0");
        bizFeesCharged.setCreatdate(sy.getHost_date());
        bizFeesCharged.setHostDate(sdf.format(new Date()));
        bizFeesCharged.setUuid(sy.getUuid());
        bizFeesCharged.setRmbStatus("2"); //未发放
        bizFeesChargedService.insertBizFeesCharged(bizFeesCharged);

        return bizFeesCharged;
    }

    @Override
    public List<BizTransactionAmountEntity> selectAllAmount() {
        return bizTransactionAmountMapper.selectAllAmount();
    }
    @Override
    public boolean isOrNo(SynchronizationDataDTO sy) {
        BizFeesCharged bizFeesCharged=bizFeesChargedService.selectNewOrderNo(sy.getNew_order_no());
        if(bizFeesCharged==null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void executeDayDataToThisMonthData() {
        // 将今日数据转移至本月数据表
        // 如果今日是本月第一天,则将数据转移至上月数据表
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        String date = sdf.format(rightNow.getTime());
        String firstDate = date.substring(date.length()-2);
        System.out.println(firstDate);
        List<BizTransactionAmountThisMonth> bizMerchTransactions = merchTransactionsMapper.getTodayData();
        if(iBizTransactionAmountThisMonthService.saveBatch(bizMerchTransactions)){
            //清空当日交易
            merchTransactionsMapper.cleanTodayTransData();
        }
        if("01".equals(firstDate)){
            // 当月数据转移至上月数据
        }
    }



    @Override
    public void executeDayTotalToThisMonthData() {
        // 处理代理今日
        List<SysUser> sysUserList = iSysUserService.selectAllUserList();
        for(SysUser user : sysUserList){
            System.out.println(user);
//            user.setThisMonthTransAmount(user.getThisMonthTransAmount() + user.getTodayTransAmount());
//            user.setThisMonthProfitAmount(user.getThisMonthProfitAmount() + user.getTodayProfitAmount());
            user.setYesterdayTransAmount(user.getTodayTransAmount());
            user.setYesterdayProfitAmount(user.getTodayProfitAmount());
            user.setTodayProfitAmount(0.00);
            user.setTodayTransAmount(0.00);
            System.out.println(user);
            iSysUserService.updateUserInfo(user);
        }

        // 处理商户今日
        List<BizMerchantInfo> infoList = iBizMerchantInfoService.selectAllMerchantInfoList();
        for(BizMerchantInfo info : infoList){
            System.out.println(info);
//            info.setThisMonthTransAmount(info.getThisMonthTransAmount().add(info.getTodayTransAmount()));
            info.setTodayTransAmount(BigDecimal.ZERO);
            System.out.println(info);
        }
        iBizMerchantInfoService.saveOrUpdateBatch(infoList);


        // 处理按渠道统计的交易量
        List<AizStatisticalMerchant> aizList = iAizStatisticalMerchantService.queryAllList();
        for(AizStatisticalMerchant asm : aizList){
            asm.setYesterdayProfitAmount(asm.getTodayProfitAmount());
            asm.setTodayProfitAmount(0.00);
            asm.setYesterdayTransAmount(asm.getTodayTransAmount());
            asm.setTodayTransAmount(0.00);
        }
        iAizStatisticalMerchantService.saveOrUpdateBatch(aizList);
    }



    public void unActive() throws ParseException {
        // 查询出所有 关联未激活模板的设备
        List<BizPosMachine> machineList= bizPosMachineMapper.getMachineAboutTemp1();
        for(BizPosMachine e : machineList){
            BizPosMachineStatusRecords bizPosMachineStatusRecords = iBizPosMachineStatusRecordsService.selectBizPosMachineStatusRecordsBysnCode(e.getPosCode());
            if("2".equals(bizPosMachineStatusRecords.getRecordsType())){
                continue;
            }
            UnFailureActiveTemplate unFailureActiveTemplate = unFailureActiveTemplateMapper.selectById(e.getTemp1());
            //获取
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(sdf.parse(bizPosMachineStatusRecords.getTime()));
            rightNow.add(Calendar.DAY_OF_MONTH, unFailureActiveTemplate.getDays());
            String expireDate = sdf.format(rightNow.getTime());
            System.out.println(e.getPosCode()+"扣款截止日期"+expireDate);
            if(expireDate.equals(sdf.format(new Date()))){
                //1.未激活扣款存记录 aiz_deduction_records
                AizDeductionRecords adr = new AizDeductionRecords();
                adr.setDeductionSn(e.getPosCode());
                adr.setDeductionMerchId(e.getMerchId());
                BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(e.getMerchId());
                adr.setDeductionMerchName(bizMerchant.getMerchName());
                adr.setDeductionType("1");
                adr.setDeductionAmount(unFailureActiveTemplate.getDeductionPrice().longValue());
                adr.setDeductionResult(unFailureActiveTemplate.getDays()+"天未激活");
                adr.setDeductionTime(new Date());
                iAizDeductionRecordsService.saveOrUpdate(adr);
                //2.扣款钱包 aiz_deduction_wallet
                AizDeductionWallet aizDeductionWallet = iAizDeductionRecordsService.getWalletByUserId(bizMerchant.getUserId());
                if(aizDeductionWallet == null){
                    AizDeductionWallet aizDeductionWallet_new = new AizDeductionWallet();
                    aizDeductionWallet_new.setUserId(bizMerchant.getUserId());
                    aizDeductionWallet_new.setUserName(bizMerchant.getMerchName());
                    aizDeductionWallet_new.setAmount(unFailureActiveTemplate.getDeductionPrice());
                    aizDeductionWallet_new.setCreateTime(new Date());
                    iAizDeductionRecordsService.insertWalletData(aizDeductionWallet_new);
                }else{
                    aizDeductionWallet.setAmount(aizDeductionWallet.getAmount().add(unFailureActiveTemplate.getDeductionPrice()));
                    aizDeductionWallet.setCreateTime(new Date());
                    iAizDeductionRecordsService.updateWalletData(aizDeductionWallet);
                }
                //3.未激活列表 aiz_un_active_records
                AizUnActiveRecords aizUnActiveRecords = new AizUnActiveRecords();
                aizUnActiveRecords.setMerchId(bizMerchant.getMerchId());
                aizUnActiveRecords.setMerchantName(bizMerchant.getMerchName());
                BizMerchantInfo bizMerchantInfo = iBizMerchantInfoService.getMerchantInfoBySnAndSuccess(e.getPosCode());
                aizUnActiveRecords.setMerchantCode(bizMerchantInfo.getMerchantCode());
                aizUnActiveRecords.setPosSn(e.getPosCode());
                aizUnActiveRecords.setDays(unFailureActiveTemplate.getDays());
                aizUnActiveRecords.setDeductionMoney(unFailureActiveTemplate.getDeductionPrice().doubleValue());
                iAizUnActiveRecordsService.saveOrUpdate(aizUnActiveRecords);
            }
        }
    }

    @Override
    public void executeThisMonthDataToLastMonthDate() {
        // 处理代理本月
        List<SysUser> sysUserList = iSysUserService.selectAllUserList();
        for(SysUser user : sysUserList){
            System.out.println(user);
            user.setLastMonthTransAmount(user.getThisMonthTransAmount());
            user.setLastMonthProfitAmount(user.getThisMonthProfitAmount());
            user.setThisMonthTransAmount(0.00);
            user.setThisMonthProfitAmount(0.00);
            System.out.println(user);
            iSysUserService.updateMonthUserInfo(user);
        }

        // 处理商户本月
        List<BizMerchantInfo> infoList = iBizMerchantInfoService.selectAllMerchantInfoList();
        for(BizMerchantInfo info : infoList){
            info.setLastMonthTransAmount(info.getThisMonthTransAmount());
            iBizMerchantInfoService.updateTransAmount(info);
        }

        // 处理商户本月
        List<BizMerchantInfo> infoList1 = iBizMerchantInfoService.selectAllMerchantInfoList();
        for(BizMerchantInfo info : infoList1){
            info.setThisMonthTransAmount(BigDecimal.ZERO);
            iBizMerchantInfoService.updateTransAmount1(info);
        }


        // 处理按渠道统计的月交易量
        List<AizStatisticalMerchant> aizList = iAizStatisticalMerchantService.queryAllList();
        for(AizStatisticalMerchant asm : aizList){
            System.out.println(asm);
            asm.setLastMonthTransAmount(asm.getThisMonthTransAmount());
            asm.setLastMonthProfitAmount(asm.getThisMonthProfitAmount());
            asm.setThisMonthTransAmount(0.00);
            asm.setThisMonthProfitAmount(0.00);
            System.out.println(asm);
            iAizStatisticalMerchantService.updateTransAmount(asm);
        }
//        iAizStatisticalMerchantService.saveOrUpdateBatch(aizList);// 批量更新不好使 , 垃圾
    }
}
